package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.AddressModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/26.
 */
    public class AddressAdapter extends BaseAdapter {

        private Context mContext;
        private List<AddressModel> mData;

        public AddressAdapter(Context context){
            mContext=context;
            mData=new ArrayList<>();
        }

        public AddressAdapter(Context context,List<AddressModel> data){
            mContext=context;
            mData=data;
        }

        public void setData(List<AddressModel> data){
            mData.clear();
            mData.addAll(data);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                convertView= LayoutInflater.from(mContext).inflate(R.layout.item_list_addressnoiv,parent,false);
                holder=new ViewHolder();
                holder.mTxt_name= (TextView) convertView.findViewById(R.id.item_address_name);
                holder.mTxt_phone= (TextView) convertView.findViewById(R.id.item_address_phone);
                holder.mTxt_address= (TextView) convertView.findViewById(R.id.item_address_detail);
                holder.mTxt_default= (TextView) convertView.findViewById(R.id.item_address_default);
                convertView.setTag(holder);
            }else{
                holder= (ViewHolder) convertView.getTag();
            }
            AddressModel model=mData.get(position);
            holder.mTxt_name.setText(model.getName());
            holder.mTxt_phone.setText(model.getPhone());
            holder.mTxt_address.setText(model.getProvince()+" "+model.getCity()+" "+model.getCounty()+" "+model.getAddress());
            if(model.isDefault()){
                // holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.select_icon));
                holder.mTxt_default.setVisibility(View.VISIBLE);
            }else{
                //holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.unselect_icon));
                holder.mTxt_default.setVisibility(View.GONE);
            }
            return convertView;
        }

        class ViewHolder{
            TextView mTxt_name;
            TextView mTxt_phone;
            TextView mTxt_address;
            TextView mTxt_default;
        }
    }
